/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.tool;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import top.wardyan.base.action.BaseBillShell;
import top.wardyan.base.config.DataConfig;

/**
 * @author ward
 *
 * 2019年2月21日
 */
public class AppConfigPropertiesConf extends BaseBillShell<AppConfigPropertiesConf>{
	protected Label lblName;
	protected Label lblPrefix;
	protected Text txtName;
	protected Text txtPrefix;
	protected Button btnNew;
	protected Button btnDel;
	protected String key;
	
	/** @wbp.parser.constructor */
	public AppConfigPropertiesConf(String key) {
		//setSize(600, 480);
		this(key, null, null);
	}
	
	public AppConfigPropertiesConf(String key, String name, String prefix) {
		super(1);
		this.key = key;
		this.init();
		if(name != null && prefix != null) {
			this.txtName.setText(name);
			this.txtName.setEnabled(false);
			this.txtPrefix.setText(prefix);
			this.btnDel.setEnabled(true);
		}
		this.txtName.setFocus();
	}
	
	
	
	
	protected void init() {
		this.lblName = new Label(this, SWT.NONE);
		this.lblName.setAlignment(SWT.RIGHT);
		this.lblName.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.lblName.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.lblName.setBounds(170, 96, 61, 21);
		this.lblName.setText("名称");
		
		this.lblPrefix = new Label(this, SWT.NONE);
		this.lblPrefix.setText("前缀");
		this.lblPrefix.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.lblPrefix.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.lblPrefix.setAlignment(SWT.RIGHT);
		this.lblPrefix.setBounds(187, 134, 44, 21);
		
		this.txtName = new Text(this, SWT.BORDER);
		this.txtName.setBounds(237, 94, 150, 23);
		
		this.txtPrefix = new Text(this, SWT.BORDER);
		this.txtPrefix.setBounds(237, 132, 150, 23);
		
		this.btnNew = new Button(this, SWT.NONE);
		this.btnNew.addSelectionListener(getSelectionAdapter());
		this.btnNew.setBounds(354, 164, 61, 27);
		this.btnNew.setText("新增");
		
		this.btnDel = new Button(this, SWT.NONE);
		this.btnDel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Map<String, String> rs = new HashMap<>();
				rs.put("name", txtName.getText());
				rs.put("prefix", "");
				DataConfig.put(key, rs);
				self.dispose();
			}
		});
		this.btnDel.setEnabled(false);
		this.btnDel.setText("删除");
		this.btnDel.setBounds(287, 164, 61, 27);
	}
	
	protected SelectionAdapter getSelectionAdapter() {
		return new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				Map<String, String> rs = new HashMap<>();
				rs.put("name", txtName.getText());
				rs.put("prefix", txtPrefix.getText());
				DataConfig.put(key, rs);
				self.dispose();
			}
		};
	}

	/* (non-Javadoc)
	 * @see top.wardyan.base.action.BaseBillShell#getTitle()
	 */
	@Override
	protected String getTitle() {
		return "配置";
	}
}
